package xing.repository.page;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import xing.domain.page.PageTemplate;
import xing.repository.page.custom.PageTemplateRepositoryCus;

import java.util.List;

public interface PageTemplateRepository extends JpaRepository<PageTemplate,Long>, QueryDslPredicateExecutor<PageTemplate>,PageTemplateRepositoryCus {


    @Query(value = "select *\n" +
            "\t\tfrom page_template t\n" +
            "\t\twhere (select count(1)+1 from page_template where TemplateType=t.TemplateType and Id>t.Id and CompanyCode=?1 and TemplateType>1)<=3\n" +
            "\t\tand TemplateType>1 \n" +
            "\t\tand  CompanyCode=?1 \n" +
            "\t\tORDER BY t.Id desc", nativeQuery = true)
    List<PageTemplate> findGroupedTop3ByPageTemplateType(String companyCode);

    List<PageTemplate> findByCompanycodeOrderByCreatedDesc(String companyCode);

    List<PageTemplate> findByTemplatetype(int templatetype);

    List<PageTemplate> findByCompanycodeAndTemplatetypeOrderByCreatedDesc(String companycode, int templatetype);
}
